const KoaRouter = require('koa-router');
const Template = require('../util/Template');
const HttpClient = require('../util/HttpClient')

const modules = require('../../modules');
const router = new KoaRouter();

router.get('/security/tag/serviceProvider', async ctx => {
  let modConfig = modules['serviceProvider.view'];
  let reqHeaders = ctx.request.headers;
  let script = ['/serviceProvider.view.js'];
  let result = await HttpClient.request('/security/tag/serviceProvider', 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('外部服务商', body, script, modConfig.template, ctx)
})



router.get('/security/tag/serviceProviderHome/:id', async ctx => {
  let modConfig = modules['security.tag.serviceProviderHome'];
  let reqHeaders = ctx.request.headers;
  let script = ['/security.tag.serviceProviderHome.js'];
  let result = await HttpClient.request(`/security/tag/serviceProviderHome/${ctx.params.id}`, 'get', null, { headers: reqHeaders });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('服务商主页', body, script, modConfig.template, ctx)
})

/* 服务商新建 */
router.get('/service/manage/provider/create', async ctx => {
  let modConfig = modules['serviceProvider.provider.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/serviceProvider.provider.edit.js'];
  let result = await HttpClient.request('/service/manage/provider/create', 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml( '新建服务商', body, script, modConfig.template, ctx)
})

/* 服务商编辑 */
router.get('/service/manage/provider/edit', async ctx => {
  let modConfig = modules['serviceProvider.provider.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/serviceProvider.provider.edit.js'];
  const id = ctx.request.query.id;
  let result = await HttpClient.request(`/service/manage/provider/edit?id=${id}`, 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('编辑服务商', body, script, modConfig.template, ctx)
})
/* 服务商详情 */
router.get('/service/manage/provider/detail', async ctx => {
  let modConfig = modules['serviceProvider.provider.detail'];
  let reqHeaders = ctx.request.headers;
  let script = ['/serviceProvider.provider.detail.js'];
  const id = ctx.request.query.id;
  let result = await HttpClient.request(`/service/manage/provider/detail?id=${id}`, 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('服务商详情', body, script, modConfig.template, ctx)
})

/* 工程师新建 */
router.get('/service/manage/engineer/create', async ctx => {
  let modConfig = modules['serviceProvider.engineer.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/serviceProvider.engineer.edit.js'];
  const tenantProviderId = ctx.request.tenantProviderId;
  let result = await HttpClient.request(tenantProviderId ? `/service/manage/engineer/create?tenantProviderId=${tenantProviderId}` : '/service/manage/engineer/create', 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('新建工程师', body, script, modConfig.template, ctx)
})

/* 工程师编辑 */
router.get('/service/manage/engineer/edit', async ctx => {
  let modConfig = modules['serviceProvider.engineer.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/serviceProvider.engineer.edit.js'];
  const id = ctx.request.query.id;
  const tenantProviderId = ctx.request.tenantProviderId;
  let result = await HttpClient.request(`/service/manage/engineer/edit?id=${id}&tenantProviderId=${tenantProviderId}`, 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('编辑工程师', body, script, modConfig.template, ctx)
})
/* 工程师详情 */
router.get('/service/manage/engineer/detail', async ctx => {
  let modConfig = modules['serviceProvider.engineer.detail'];
  let reqHeaders = ctx.request.headers;
  let script = ['/serviceProvider.engineer.detail.js'];
  const id = ctx.request.query.id;
  let result = await HttpClient.request(`/service/manage/engineer/detail?id=${id}`, 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('工程师详情', body, script, modConfig.template, ctx)
})
module.exports = router;

/*服务商管理*/
router.get('/serviceManage', async ctx => {
  let script = ['/serviceManage.list.js'];
  let modConfig = modules['serviceManage.list'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/service/manage/provider/serviceManage', 'get', null, {headers: reqHeaders,});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('服务商管理', body, script, modConfig.template, ctx);
});